package org.infosys.user.service.impl;

import org.infosys.basic.constant.BaseConstants;
import org.infosys.basic.exception.BusinessException;
import org.infosys.basic.utils.MD5Utils;
import org.infosys.basic.utils.StrUtils;
import org.infosys.basic.vo.RegisterVo;
import org.infosys.user.domain.Logininfo;
import org.infosys.user.domain.User;
import org.infosys.user.mapper.LogininfoMapper;
import org.infosys.user.mapper.UserMapper;
import org.infosys.user.service.IUserService;
import org.infosys.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hank
 * @since 2022-07-06
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
    * @Title: phoneCodeRegister
    * @Description: 手机注册
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/6 18:22
    * @Parameters: [registerVo]
    * @Return void
    */
    @Override
    public void phoneCodeRegister(RegisterVo registerVo) {
        // 参数非空校验
        String phone = registerVo.getPhone();
        String smsCodeValue = registerVo.getSmsCodeValue();
        String password = registerVo.getPassword();
        String confirmPassword = registerVo.getConfirmPassword();
        if (StringUtils.isEmpty(phone)) {
            throw new BusinessException("请输入电话号码！");
        }
        if (StringUtils.isEmpty(smsCodeValue)) {
            throw new BusinessException("请输入验证码！");
        }
        if (StringUtils.isEmpty(password)) {
            throw new BusinessException("请输入密码！");
        }
        if (StringUtils.isEmpty(confirmPassword)) {
            throw new BusinessException("请输入确认密码！");
        }
        // 两次密码是否一致
        if (!password.equals(confirmPassword)) {
            throw new BusinessException("两次密码不一致！");
        }
        // 验证短信验证码是否过期
        String smsCodeKey = BaseConstants.VerifyCodeConstant.BUSINESS_REGISTER_KEY + phone;
        Object smsCodeValueTemp = redisTemplate.opsForValue().get(smsCodeKey);
        if (ObjectUtils.isEmpty(smsCodeValueTemp)) {
            throw new BusinessException("验证码已过期，请重新输入");
        }
        // 验证验证码和redis中的是否一致
        String[] split = smsCodeValueTemp.toString().split(":");
        String smsCode = split[1];
        if (!smsCodeValue.equalsIgnoreCase(smsCode)) {
            throw new BusinessException("验证码错误，请重新输入1");
        }
        // 入库
        User user = registerVo2User(registerVo);
        userMapper.save(user);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);

    }

    /**
    * @Title: user2Logininfo
    * @Description: 拷贝user到logininfo
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/6 18:51
    * @Parameters: [user]
    * @Return org.infosys.user.domain.Logininfo
    */
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }

    /**
    * @Title: registerVo2User
    * @Description: 初始化user
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/6 18:51
    * @Parameters: [registerVo]
    * @Return org.infosys.user.domain.User
    */
    private User registerVo2User(RegisterVo registerVo) {
        User user = new User();
        user.setPhone(registerVo.getPhone());
        user.setUsername(registerVo.getPhone());
        // 加盐
        String salt = StrUtils.getComplexRandomString(32);
        // 加密
        String md5Pwp = MD5Utils.encrypByMd5(registerVo.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwp);
        return user;
    }
}
